const query = require('../../utils/DB')
class userModel {
    // 判断用户表中 是否存在 这个用户 
    static async getOpenId(openid) {
        let res = await query(`select count(*) as num from ly_member where wxopenid='${openid}'`)
        if (res[0]['num'] > 0) {
            return true
        } else {
            return false
        }
    }

    // 根据openid 获取用户信息
    static async getUser(openid) {
        let res = await query(`select * from ly_member where wxopenid='${openid}'`)
        return res[0]
    }

    // 添加用户 
    static async addUser(data) {
        await query(`INSERT INTO ly_member(wxopenid,nickname) VALUES ('${data.openid}','${data.nickname}')`)
        return true
    }

    static async editUser(data, type) {
        return await query(`UPDATE ly_member SET ${type}='${data[type]}' WHERE id='${data.uid}'`)
    }

    static async doFollow(uid, buid, nickname) {
        // 先查询 是否用户已经关注了 当前人
        let res = await query(`SELECT * FROM ly_guanzhu WHERE uid='${uid}' AND buid='${buid}'`)
        if (res.length > 0) {
            // 取消关注后 我关注的人的状态也要改成互关
            await query(`UPDATE ly_guanzhu SET huguan='1' WHERE uid='${buid}' AND buid='${uid}'`)
            // 如果已经关注了 就取消关注
            return await query(`DELETE FROM ly_guanzhu WHERE uid='${uid}' AND buid='${buid}'`)
        } else {
            // 查询一个 我要关注的人的 昵称
            let res2 = await query(`SELECT nickname FROM ly_member WHERE id='${buid}'`)
            let bnickname = res2[0]['nickname']
            // 查询我要关注的这个人 是不是也关注我了 
            let res3 = await query(`SELECT * FROM ly_guanzhu WHERE uid='${buid}' AND buid='${uid}'`)
            let huguan = 1
            if (res3.length > 0) {
                huguan = 2
                // 如果是互相关注 把关注的我的人的状态也改成互关
                await query(`UPDATE ly_guanzhu SET huguan='${huguan}' WHERE uid='${buid}' AND buid='${uid}'`)
            }
            return await query(`INSERT INTO ly_guanzhu(uid,buid,nickname,bnickname,huguan,bheaderurl) VALUES ('${uid}','${buid}','${nickname}','${bnickname}','${huguan}','${res2[0]['headerurl'] ?? ""}')`)
        }
    }
    // 获取 我是否 已经关注了这个用户
    static async getFollow(uid, buid) {
        return await query(`SELECT * FROM ly_guanzhu WHERE uid='${uid}' AND buid='${buid}'`)
    }

    // 获取一下我关注的人的列表
    static async getFollowList(uid, nowPage, pageSize) {
        // 分页查询
        let start = (nowPage - 1) * pageSize
        let res = await query(`SELECT *,(SELECT COUNT(*) FROM ly_chat WHERE ly_chat.get_uid=ly_guanzhu.uid and ly_chat.send_uid=ly_guanzhu.buid and ly_chat.is_red=2) as nums FROM ly_guanzhu WHERE uid='${uid}' LIMIT ${start},${pageSize}`)

        for (let i = 0; i < res.length; i++) {
            let content = await query(`select content from ly_chat where send_uid='${res[i]['buid']}' and get_uid='${uid}'  order by send_time desc limit 1`)
            res[i]['content'] = content?.length > 0 ? content[0]['content'] : ""
        }
        // 获取总的数据条数
        let count = await query(`SELECT COUNT(*) as count FROM ly_guanzhu WHERE uid='${uid}'`)
        return { list: res, count: count[0]['count'] }
    }

    // 根据uid 获取关注我的所有列表
    static async getFansList(uid, nowPage, pageSize) {
        // 分页查询
        let start = (nowPage - 1) * pageSize
        let res = await query(`SELECT * FROM ly_guanzhu WHERE buid='${uid}' LIMIT ${start},${pageSize}`)
        // 获取总的数据条数
        let count = await query(`SELECT COUNT(*) as count FROM ly_guanzhu WHERE buid='${uid}'`)
        return { list: res, count: count[0]['count'] }

    }

    static async getFriendList(uid) {
        // 分页查询
        let res = await query(`SELECT * FROM ly_guanzhu WHERE uid='${uid}' or buid='${uid}'`)
        return res
    }

    static async addChat(data) {
        // 查询用户表 获取用户信息
        let senduser = await query(`SELECT * FROM ly_member WHERE id='${data.send_uid}'`)
        let getuser = await query(`SELECT * FROM ly_member WHERE id='${data.get_uid}'`)
        return await query(`INSERT INTO ly_chat(send_uid, get_uid, send_time, send_nickname, get_nickname, send_img, get_img, content) VALUES ('${data.send_uid}', '${data.get_uid}', '${data.send_time}', '${senduser[0]['nickname']}', '${getuser[0]['nickname']}', '${senduser[0]['headerurl']}', '${getuser[0]['headerurl']}', '${data.content}')`)
    }

    static async getChatList(send_uid, get_uid) {
        return await query(`SELECT * FROM ly_chat WHERE (send_uid='${send_uid}' AND get_uid='${get_uid}') or (send_uid='${get_uid}' AND get_uid='${send_uid}')  ORDER BY send_time ASC`)
    }

    static async setRead(uid, buid){
        console.log(`UPDATE ly_chat SET is_red=1 WHERE get_uid='${uid}' AND send_uid='${buid}'`)
        return await query(`UPDATE ly_chat SET is_red=1 WHERE get_uid='${uid}' AND send_uid='${buid}'`)
    }
}

module.exports = userModel